package com.allen.mapper;

import com.allen.bean.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

@Mapper //标记mapper文件位置，否则在Application.class启动类上配置mapper包扫描
@Repository
public interface UserMapper {

    /**
     * 查询用户名是否存在，存在则不允许注册
     * 注解@Param(value),若value与可变参数相同，则注解可省略
     * 注解@Request,列名和字段名相同，注解可省略
     * @param username
     * @return
     */
    @Select(value="select*from user where username=#{username}")
    @Results
            ({@Result(property = "username",column = "username"),   //@Result：为了解决数据库字段名与实体类属性名不一致的问题
                    @Result(property = "pwd",column = "pwd")})      //column:数据库字段名，property：实体类属性名

    User findUserByName(@Param("username") String username);

    /**
     * 注册  插入一条user记录
     * @param user
     *  @return
     */

    @Insert("insert into user values(#{id},#{username},#{pwd})")
    //加入该注解可以保存对象后，查看对象插入id
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void regist(User user);

    /**
     * 登录
     * @param user
     * @return
     */
    @Select("select id from user where username = #{username} and pwd = #{pwd}")
    Long login(User user);

}
